package com.example.sourcecodelearning.proxy.factory;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Console;
import lombok.AllArgsConstructor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
 * @author yuzhenxin
 * @date 2023-07-30 10:53
 */
@AllArgsConstructor
public class JdkProxy implements InvocationHandler {

    // target
    private Object targetObject;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        StopWatch stopWatch = new StopWatch("Jdk动态代理任务");

        System.out.println("准备执行Jdk动态代理");
        stopWatch.start();

        Object ret = method.invoke(targetObject, args);

        System.out.println("Jdk动态代理执行完成");
        stopWatch.stop();

        Console.log("任务执行耗时：{}毫秒", stopWatch.getTotal(TimeUnit.MILLISECONDS));

        // 代理方法返回值
        return ret;
    }
}
